package logic

import (
	"context"
	"errors"
	"person/model/mysql"

	"person/goods/goods"
	"person/goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailLogic {
	return &DetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DetailLogic) Detail(in *goods.DetailRequest) (*goods.DetailResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	err := user.FindUserByMobile(mysql.DB, in.Mobile)
	if err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("用户未注册")
	}
	if in.Password != user.Password {
		return nil, errors.New("密码错误")
	}

	return &goods.DetailResponse{
		Mobile:   user.Mobile,
		Password: user.Password,
		UserId:   int64(user.ID),
	}, nil
}
